package com.linuxgt.baidu.zhidao;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by LIUTENG on 2015-05-17.
 */
public class ExportExcel {
    private final String format = "xlsx";

    public void exportExcel(OutputStream os) throws Exception {
        Workbook workbook = null;
        if ("xls".equals(format)) {
            workbook = new HSSFWorkbook();
        }
        if ("xlsx".equals(format)) {
            workbook = new XSSFWorkbook();
        }
        List<Classroom> classrooms;
        ArrangeService aS = new ArrangeServiceImpl();
        classrooms = aS.findAllClassroom();//每个考场一个sheet
        for (int i = 0; i < classrooms.size(); i++) {

            List<Arrange> list = aS.findByClassroomId(classrooms.get(i).getId());
            if (list.size() != 0) {
                Sheet sheet = workbook.createSheet(list.get(0).getCourseName() + "_" + list.get(0).getClassroomId());

                CellStyle cellStyle1 = workbook.createCellStyle(); //建立新的cell样式

                Font font1 = workbook.createFont();
                font1.setFontName("宋体");
                font1.setFontHeightInPoints((short) 16);// 设置字体大小
                font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
                cellStyle1.setFont(font1);
//                cellStyle1.setAlignment(CellStyle.ALIGN_CENTER);
                cellStyle1.setAlignment(CellStyle.ALIGN_CENTER);

                Row row = sheet.createRow(0);
                row.createCell(0).setCellStyle(cellStyle1);
                row.getCell(0).setCellValue("考试科目：");
                row.createCell(1).setCellStyle(cellStyle1);
                row.getCell(1).setCellValue(list.get(0).getCourseName());

                Row row3 = sheet.createRow(1);
                row3.createCell(0).setCellStyle(cellStyle1);
                row3.getCell(0).setCellValue("序号");
                row3.createCell(1).setCellStyle(cellStyle1);
                row3.getCell(1).setCellValue("学号");
                row3.createCell(2).setCellStyle(cellStyle1);
                row3.getCell(2).setCellValue("学生");
                row3.createCell(3).setCellStyle(cellStyle1);
                row3.getCell(3).setCellValue("备注");

                for (int j = 2; j < list.size() + 2; j++) {
                    Arrange a = list.get(j - 2);
                    row = sheet.createRow(j);
                    row.setRowStyle(cellStyle1);
                    row.createCell(0).setCellValue(j - 1);
                    row.createCell(1).setCellValue(a.getStuId());
                    row.createCell(2).setCellValue(a.getStudentName());
                    row.createCell(3).setCellValue("");
                }

            }

        }
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        String desktopDir = System.getProperty("user.home");
        File file = new File(desktopDir + "\\Desktop\\ExcelTest.xlsx");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ExportExcel exportExcel = new ExportExcel();
        exportExcel.exportExcel(fileOutputStream);
    }
}
